import Vue from "vue";
import Vuex from "vuex";
import { getStorage, removeStorage } from "../utils/storage";
import {
  reqGetCartApi,
  toggleSigleCartGoods,
  toggleAllCartGoods,
  reqGoodsNumsAdd,
  reqGoodsNumsReduce,
  reqDelCartGoods,
  reqAddCartGoods,
} from "@/services";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    user: null,
    cartList: [],
  },
  mutations: {
    //建议使用mutations中的方法修改state
    initUser(state) {
      state.user = getStorage("user");
    },
    logout(state) {
      removeStorage("user");
      state.user = null;
    },
    initCart(state, goodList) {
      state.cartList.push(...goodList);
    },
    toggleSigleCartItem(state, id) {
      const { cartList } = state;
      const index = cartList.findIndex((item) => item.id === id);
      // console.log(index);
      if (index !== -1) {
        cartList[index].selected = !cartList[index].selected;
      }
    },
    toggleAllGoodsSelect(state, flag) {
      const { cartList } = state;
      for (let index = 0; index < cartList.length; index++) {
        cartList[index].selected = flag;
      }
    },
    GoodsNumsAdd(state, id) {
      const { cartList } = state;
      const index = cartList.findIndex((item) => item.id === id);
      cartList[index].goods_nums++;
    },
    GoodsNumsReduce(state, id) {
      const { cartList } = state;
      const index = cartList.findIndex((item) => item.id === id);
      cartList[index].goods_nums--;
    },
    delectCartGoods(state, str_id) {
      const { cartList } = state;
      const list_id = str_id.split(",");
      // console.log(list_id);
      // console.log(cartList, list_id);
      list_id.forEach((id) => {
        const index = cartList.findIndex((item) => item.id == id);
        console.log(index);
        if (index != -1) {
          cartList.splice(index, 1);
        }
      });
    },
  },
  actions: {
    //请求购物车的数据
    async initCart({ commit, dispatch }, { page, size }) {
      const res = await reqGetCartApi(page, size);
      // console.log(res);
      if (res.code === 200) {
        const { list, total, page } = res.data;
        commit("initCart", list);
        if (page < total) {
          dispatch("initCart", { page: page + 1, size });
        }
      }
    },
    //修改单个对象的选中状态
    async toggleSigleCartGoods({ commit }, id) {
      const res = await toggleSigleCartGoods(id);

      if (res.code === 200) {
        commit("toggleSigleCartItem", id);
      }
    },
    //修改所有商品的选中状态
    async toggleAllGoodsSelect({ commit }, flag) {
      const res = await toggleAllCartGoods(flag);
      // console.log(res);
      if (res.code === 200) {
        commit("toggleAllGoodsSelect", flag);
      }
    },
    //购物车商品数量加一
    async reqGoodsNumsAdd({ commit }, id) {
      const res = await reqGoodsNumsAdd(id);
      if (res.code === 200) {
        commit("GoodsNumsAdd", id);
      }
    },
    //购物车商品数量减一
    async reqGoodsNumsReduce({ commit }, id) {
      const res = await reqGoodsNumsReduce(id);
      if (res.code === 200) {
        commit("GoodsNumsReduce", id);
      }
    },
    //删除购物车商品
    async delectCartGoods({ commit }, id) {
      const res = await reqDelCartGoods(id);
      // console.log(res);
      if (res.code === 200) {
        commit("delectCartGoods", id);
      }
    },
    // 购物车商品添加
    async addCartGoods({ getters }, { goodsId, nums }) {
      // console.log(goodsId, nums);
      const res = await reqAddCartGoods(goodsId, nums);
      if (res.code == 200) {
        getters("countCartSelect");
      }
    },
  },
  getters: {
    login(state) {
      const { user } = state;
      return !!(user && user.token);
    },
    isAllSecect(state) {
      const { cartList } = state;
      return cartList.findIndex((item) => !item.selected) < 0;
    },
    //计算购物车选中的总数
    countCartSelect(state) {
      const { cartList } = state;
      return cartList.filter((item) => item.selected).length;
    },

    //计算购物车总价格
    totalCart(state) {
      const { cartList } = state;
      return cartList
        .filter((item) => item.selected)
        .map((item) => item.goods_nums * item.price)
        .reduce((prev, cur) => prev + cur, 0);
    },
  },
});

export default store;
